package gmusic.ma.bo;

import gmusic.bo.ReleaseBean;
import gmusic.services.impl.GMusicServicesImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ReleaseMABean extends ReleaseBean {

	public static final String PREFIXE_URL_RELEASE_COVER = "http://www.metal-archives.com/images/";
	public static final String URLCoverColumnName = "url_cover";
	protected String URLCover;

	public static final String PREFIXE_URL_RELEASE = "http://www.metal-archives.com/albums/";
	public static final String urlColumnName = "url";
	private String url = null;

	private List<TrackMABean> tracksMA = new ArrayList<TrackMABean>();

	public ReleaseMABean() {
	}

	public ReleaseMABean(int maid, long groupeId, String nom, int typeId,
			int annee) {
		setMaid(maid);
		setArtistMaId(groupeId);
		setNom(nom);
		setTypeId(typeId);
		setAnnee(annee);
	}

	public ReleaseMABean(ResultSet rs) throws SQLException {
		setMaid(rs.getLong("maid"));
		setNom(rs.getString("nom"));
		setArtistMaId(rs.getLong("groupe_maid"));
		setAnnee(rs.getInt("annee"));
		setTypeId(rs.getInt("type_id"));
		setURLCover(rs.getString(URLCoverColumnName));
		setUrl(rs.getString(urlColumnName));
		setReview(rs.getString(reviewColumnName));
	}

	public String toString() {
		return "[maid=" + maid + ";nom=" + nom + ";annee=" + annee + ";type="
				+ typeId + "]";
	}

	public String toString4Console() {
		return maid + "\t" + annee + " " + nom;
	}

	public List<TrackMABean> getTracksMA() {
		return tracksMA;
	}

	public void setTracksMA(List<TrackMABean> tracks) {
		this.tracksMA = tracks;
	}

	public void addTrack(TrackMABean mt) {
		tracksMA.add(mt);
	}

	public boolean existsInGM() throws SQLException {
		return GMusicServicesImpl.getInstance().releaseMAExists(maid);
	}

	public String getUrlRelative() {
		return url;
	}

	public String getUrlAbsolue() {
		return PREFIXE_URL_RELEASE + url;
	}

	public void setUrl(String url) {
		if (url != null) {
			url = url.replace(PREFIXE_URL_RELEASE, "");
		}
		this.url = url;
	}

	/** @deprecated */
	public String getURLCover() {
		return URLCover;
	}

	public String getURLCoverRelative() {
		return URLCover;
	}

	public String getURLCoverAbsolue() {
		return PREFIXE_URL_RELEASE_COVER + URLCover;
	}

	public void setURLCover(String cover) {

		if (cover == null || (cover != null && cover.length() == 0)) {
			URLCover = null;
		} else {
			URLCover = cover.replace(PREFIXE_URL_RELEASE_COVER, "");
		}
	}
}
